package com.redtea.studyspringboot.controller;

import com.redtea.studyspringboot.domain.entity.TabUser;
import com.redtea.studyspringboot.service.TabUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

/**
 * (TabUser)表控制层
 *
 * @author makejava
 * @since 2024-04-18 10:43:13
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("tabUser")
public class TabUserController {
    /**
     * 服务对象
     */
    private final TabUserService tabUserService;

    /**
     * 分页查询
     * @param tabUser
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping
    public ResponseEntity<Page<TabUser>> queryByPage(TabUser tabUser, Integer pageNum,Integer pageSize) {
//        PageRequest pageRequest = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.ASC,"age"));
        PageRequest pageRequest = PageRequest.of(pageNum, pageSize, Sort.by(Sort.Direction.DESC,"id"));
        return ResponseEntity.ok(this.tabUserService.queryByPage(tabUser, pageRequest));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<TabUser> queryById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.tabUserService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param tabUser 实体
     * @return 新增结果
     */
    @PostMapping
    public ResponseEntity<TabUser> add(TabUser tabUser) {
        return ResponseEntity.ok(this.tabUserService.insert(tabUser));
    }

    /**
     * 编辑数据
     *
     * @param tabUser 实体
     * @return 编辑结果
     */
    @PutMapping
    public ResponseEntity<TabUser> edit(TabUser tabUser) {
        return ResponseEntity.ok(this.tabUserService.update(tabUser));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping
    public ResponseEntity<Boolean> deleteById(Long id) {
        return ResponseEntity.ok(this.tabUserService.deleteById(id));
    }

}

